package org.six11.olive3.picturephone.server.handler;

import java.io.PrintWriter;
import java.util.Map;

import org.six11.olive3.net.Message;
import org.six11.olive3.net.MessageHandler;
import org.six11.olive3.net.MessageNames;
import org.six11.olive3.picturephone.server.MessageQueue;
import org.six11.olive3.picturephone.server.ServerStateConstants;
import org.six11.olive3.server.DatabaseThing;
import org.six11.olive3.util.MultiState;

public class InitMessageHandler extends MessageHandler {

  private DatabaseThing db;
  private MultiState programState;
  private ScoreHandler scoreHandler;
  
  public InitMessageHandler(DatabaseThing db, MultiState programState, ScoreHandler scoreHandler) {
    this.db = db;
    this.programState = programState;
    this.scoreHandler = scoreHandler;
  }

  @SuppressWarnings("unchecked")
  public void handleLater(Message m) {
    String uid = m.getParamValue(MessageNames.PARAM_USER_ID);
    Map<String, MessageQueue> queues = (Map<String, MessageQueue>) programState
        .getValue(ServerStateConstants.MESSAGE_QUEUES);
    queues.put(uid, new MessageQueue(uid));
    bug("Made a message queue for " + uid);
    scoreHandler.enqueueScoreForUser(uid, null);
  }
}
